import json

from channels.generic.websocket import WebsocketConsumer
from channels.exceptions import StopConsumer
from order.models import Order
from order.serializers import OrderSerializer


class ChatConsumer(WebsocketConsumer):
    last_id = 0

    def OrderDecide(self):
        zero_orders = Order.objects.filter(is_complete=0)
        # one_orders = Order.objects.filter(is_complete=1)
        two_orders = Order.objects.filter(is_complete=2)
        if two_orders.count() != 0:
            serializer = OrderSerializer(two_orders, many=True)
            order_data = serializer.data[0]
            id = order_data['id']
            product_name = order_data['product_name']
            return id, product_name
        elif two_orders.count() == 0 and zero_orders.count() == 0:
            self.send("已完成全部订单")
            self.close()
            id = 0
            product_name = "无订单"
            return id, product_name
        else:
            serializer = OrderSerializer(zero_orders, many=True)
            order_data = serializer.data[0]
            product_name = order_data['product_name']
            id = order_data['id']
            return id, product_name

    def Execution(self, id, product_name):
        order = Order.objects.get(id=id)
        print(order)
        order.is_complete = 2
        order_type = order.order
        name = order.name
        order.save()
        if product_name == 'Product 1':
            dict = {
                'success': True,
                'plan': [4, 3, 2, 1],
                'order_type': order_type,
                'name': name
            }
            data = json.dumps(dict, ensure_ascii=False)
            self.send(data)
        elif product_name == 'Product 2':
            dict = {
                'success': True,
                'plan': [3, 4, 1, 2],
                'order_type': order_type,
                'name': name
            }
            data = json.dumps(dict, ensure_ascii=False)
            self.send(data)
        elif product_name == 'Product 3':
            dict = {
                'success': True,
                'plan': [2, 1, 3, 4],
                'order_type': order_type,
                'name': name
            }
            data = json.dumps(dict, ensure_ascii=False)
            self.send(data)
        elif product_name == 'Product 4':
            dict = {
                'success': True,
                'plan': [1, 2, 3, 4],
                'order_type': order_type,
                'name': name
            }
            data = json.dumps(dict, ensure_ascii=False)
            self.send(data)
        else:
            dict = {
                'success': False,
                'txt': "此订单信息不全"
            }
            data = json.dumps(dict, ensure_ascii=False)
            self.send(data)

    def websocket_connect(self, message):
        self.accept()
        self.send("这是订单状态监控模块，你可通过发送“开始”进行监控，发送“完成”进入下一个订单")

    def websocket_receive(self, message):
        info = message['text']
        if info == "开始":
            id, productname = self.OrderDecide()
            print(id, productname)
            self.Execution(id, productname)
            ChatConsumer.last_id = id
        if info == "完成":
            order = Order.objects.get(id=ChatConsumer.last_id)
            order.is_complete = 1
            order.save()
            id, productname = self.OrderDecide()
            if id == 0:
                print("完成所有订单了")
            else:
                self.Execution(id, productname)
            ChatConsumer.last_id = id
        if info == "结束":
            self.close()

    def websocket_disconnect(self, message):
        raise StopConsumer()
